﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSOAPUI.Lib;

namespace WebSOAPUI
{
    public partial class Download : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var attaID = Request.QueryString["aid"].ToInt();
            using (var dbContext = new CNOOCDbContext())
            {
                var attaObj = dbContext.WebServiceAttachments.Find(attaID);
                if (attaObj != null)
                {
                    System.Web.HttpResponse response = Response;
                    response.Clear();
                    response.Buffer = true;
                    response.Charset = "utf-8";
                    response.AppendHeader("content-type", "utf-8");
                    response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(attaObj.FileName));
                    response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                    response.ContentType = "file";
                    //page.EnableViewState = false;
                    response.BinaryWrite(attaObj.BinData);
                    response.Flush();
                    response.End();
                    System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
                } else
                {
                    Response.StatusCode = 404;
                }
            }

        }
    }
}